package com.cisetech.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cisetech.service.GlobalConfigService;
import com.cisetech.vo.globalconfig.GlobalConfigVo;


/**
 * 
 * @author zhaopeng
 * @date Apr 21, 2015
 */
@Controller
public class CoreController {
	private final static Logger log = LoggerFactory.getLogger(CoreController.class);
    @Autowired
    private GlobalConfigService globalConfigService;
    private GlobalConfigVo globalConfigVo = new GlobalConfigVo();
  // @Autowired
   //private QuoteToProposal qtp;
	@RequestMapping(value="/core/show",method=RequestMethod.GET)
	public String show(HttpServletRequest request,HttpServletResponse response){
		return "car/index";
	}
	@RequestMapping(value="/testToken",method=RequestMethod.POST)
	public void testToken(HttpServletRequest request,HttpServletResponse response){
		String r = globalConfigService.getAccessToken();
		try {
			PrintWriter out = response.getWriter(); 
			out.print(r);
			out.close();
			out = null;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//添加数据
	@RequestMapping(value="/adduser",method=RequestMethod.POST)
	public String addUser(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
			String configkey = request.getParameter("configkey");
			String configvalue = request.getParameter("configvalue");
			globalConfigVo.setConfigKey(configkey);
			globalConfigVo.setConfigValue(configvalue);
			globalConfigVo.setCreateTime("123");
			globalConfigVo.setUpdateTime(456+"");
			globalConfigService.insertGlobalConfig(globalConfigVo);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "login";
	}
	//查询数据
	@RequestMapping(value="/getglobal",method=RequestMethod.POST)
	public void getglobal(HttpServletRequest request,HttpServletResponse response){
		globalConfigVo.setConfigKey("nihao");
		
		globalConfigVo = globalConfigService.getGlobalConfig();
		System.out.println(globalConfigVo.getConfigValue());
	}
	//删除表数据
	@RequestMapping(value="/deleteglobal",method=RequestMethod.POST)
	public String deletglobal(HttpServletRequest request,HttpServletResponse response){
		try {
			request.setCharacterEncoding("utf-8");
			String akey = request.getParameter("akey");
			globalConfigVo.setConfigKey(akey);
			globalConfigService.deleteGlobalConfig(globalConfigVo);
			request.setAttribute("go", "删除成功");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "login";
	}
	//查询列表
	@RequestMapping(value="/getlistglobal")
	public String getlistglobal(HttpServletRequest request,HttpServletResponse response){
		List<GlobalConfigVo> list = globalConfigService.getlistGlobalConfig();
		request.setAttribute("list", list);
		return "listglobal";
	}
}
